package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import entities.*;
import relations.Participate;
import service.*;
import service.album.AlbumService;
import service.album.AlbumServiceFactory;
import service.artist.ArtistService;
import service.artist.ArtistServiceFactory;
import service.participate.ParticipateService;
import service.participate.ParticipateServiceFactory;
/**
 * Servlet implementation class searchStudentServlet
 */
public class indexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public indexServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		//Anadir Random de 15
		Long sid = 1L;
		Long sid2 = 2L;
		Long sid3 = 3L;
		Long parti1 = 1L;
		Long parti2 = 2L;
		Long parti3 = 3L;
		Album album = null;
		Album album2 = null;
		Album album3 = null;
		Participate par = null;
		Participate par2 = null;
		Participate par3 = null;
		Artist artist = null;
		Artist artist2 = null;
		Artist artist3 = null;
		String addr = null;
		
		// Get the factory to create services. This is obtained from the web.xml configuration file
		// from parameter ServiceFactoryType
		AlbumServiceFactory factory = AlbumServiceFactory.getFactory(ServletInitParams.getServiceFactoryType(this));
		// Get an instance of the album service, as given by the factory
		AlbumService service = factory.getInstance();
		
		ParticipateServiceFactory factory2 = ParticipateServiceFactory.getFactory(ServletInitParams.getServiceFactoryType(this));
		ParticipateService service2 = factory2.getInstance();
		
		ArtistServiceFactory factory3 = ArtistServiceFactory.getFactory(ServletInitParams.getServiceFactoryType(this));
		ArtistService service3 = factory3.getInstance();
		
		// Find the album
		album = service.findAlbum(sid);
		album2 = service.findAlbum(sid2);
		album3 = service.findAlbum(sid3);
		par = service2.findParticipate(1L, 1L);
		par2 = service2.findParticipate(2L, 2L);
		par3 = service2.findParticipate(3L, 3L);
		artist = service3.findArtist(1L);
		artist2 = service3.findArtist(2L);
		artist3 = service3.findArtist(3L);
		
		if (album == null && album2 == null && album3 == null){
			addr = "./albumNotFound.jsp";
		}
		else {
			session.setAttribute("album", album);
			session.setAttribute("album2", album2);
			session.setAttribute("album3", album3);
			session.setAttribute("artist", artist);
			session.setAttribute("artist2", artist2);
			session.setAttribute("artist3", artist3);
			
			addr = "index.jsp";
		}
		// forward the request to the page previously determined
		RequestDispatcher dispatcher = request.getRequestDispatcher(addr);
		dispatcher.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
